import router from '@/router';
import { MMessage } from './libs';

router.beforeEach(to => {
  if (to.meta.user == null) {
    return;
  }

  let userInfo = null;
  try {
    userInfo = JSON.parse(localStorage.getItem('user') as string);
  } catch (err) {
    userInfo = null;
  }
  if (userInfo && userInfo.token) {
    return true;
  }

  MMessage({
    content: '请先登录',
    type: 'warn'
  });
  return '/';
});
